package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.exception.ServiceException;
import com.sky.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Author tzl
 * @Date 2023-07-07 17:22:45  星期五
 * @Description: 自动填充切面
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    @Pointcut("@annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段的自动填充。。。");
        /*
         * 获取到当前对象被拦截的方法上的数据库操作类型
         */
        //方法签名对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取方法上的注解对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        //获取注解的数据库操作类型
        OperationType operationType = autoFill.value();

        /*
         * 获取到当前被拦截的方法的参数--实体对象
         */
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object entity = args[0];

        //获取公共字段的属性值
        Date nowDate = DateUtils.getNowDate();
        String userName = BaseContext.getCurrentUserName();

        /*
         * 根据当前不同的操作类型，为对应的属性通过反射来赋值
         */
        if (operationType == OperationType.INSERT){
            /*
             *为四个公共字段进行赋值
             */
            try {
                //获取到对象的set方法
                Method setCreateBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_BY, String.class);
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, Date.class);
                Method setUpdateBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_BY, String.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, Date.class);

                //通过反射给对象的属性赋值
                setCreateBy.invoke(entity,userName);
                setCreateTime.invoke(entity,nowDate);
                setUpdateBy.invoke(entity,userName);
                setUpdateTime.invoke(entity,nowDate);

            }catch (Exception e){
                throw new ServiceException(e.getMessage());
            }
        }else if (operationType == OperationType.UPDATE){
            //为两个公共字段进行赋值
            try {
                //获取到对象的set方法
                Method setUpdateBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_BY, String.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, Date.class);

                //通过反射给对象的属性赋值
                setUpdateBy.invoke(entity,userName);
                setUpdateTime.invoke(entity,nowDate);

            }catch (Exception e){
                throw new ServiceException(e.getMessage());
            }

        }else if (operationType == OperationType.DELETE){
            //为两个个公共字段进行赋值
            try {
                //获取到对象的set方法
                Method setUpdateBy = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_BY, String.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, Date.class);

                //通过反射给对象的属性赋值
                setUpdateBy.invoke(entity,userName);
                setUpdateTime.invoke(entity,nowDate);

            }catch (Exception e){
                throw new ServiceException(e.getMessage());
            }
        }

    }
}
